package com.yiwei.zxp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.yiwei.zxp.R;
import com.yiwei.zxp.api.model.AddressListsModel;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class MyAddressAdapter extends ItemAdapter<AddressListsModel.DatasBean, MyAddressAdapter.DefaultHolder> {


    public MyAddressAdapter(Context context) {
        super(context);
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_my_address, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        AddressListsModel.DatasBean item = getItem(position);
        holder.item = item;
        holder.txtNamePhone.setText(item.name + "    " + item.phone);
        holder.txtAddress.setText(item.headAddress + item.address);
        if (item.is_default == 1) {
            holder.imgDefault.setImageResource(R.mipmap.img_check_box_check);
        } else {
            holder.imgDefault.setImageResource(R.mipmap.img_check_box_uncheck);
        }
    }

    class DefaultHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        AddressListsModel.DatasBean item;
        @BindView(R.id.txt_name_phone)
        TextView txtNamePhone;
        @BindView(R.id.txt_address)
        TextView txtAddress;
        @BindView(R.id.img_default)
        ImageView imgDefault;
        @BindView(R.id.btn_edit)
        SuperButton btnEdit;
        @BindView(R.id.btn_delete)
        SuperButton btnDelete;
        @BindView(R.id.layout_total)
        LinearLayout layoutTotal;

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
//            imgDefault.setOnClickListener(this);
            btnEdit.setOnClickListener(this);
            btnDelete.setOnClickListener(this);
            layoutTotal.setOnClickListener(this);
            imgDefault.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_edit:
                    if (onChooseListener != null) {
                        onChooseListener.onEdit(item);
                    }
                    break;
                case R.id.btn_delete:
                    if (onChooseListener != null) {
                        onChooseListener.onDelete(item.id);
                    }
                    break;
                case R.id.layout_total:
                    if (onChooseListener != null) {
                        onChooseListener.onSelect(item);
                    }
                    break;
                case R.id.img_default:
                    if (onChooseListener != null) {
                        onChooseListener.onDefault(item);
                    }
                    break;

            }
        }
    }

    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onSelect(AddressListsModel.DatasBean data);

        void onDelete(String id);

        void onEdit(AddressListsModel.DatasBean data);

        void onDefault(AddressListsModel.DatasBean data);
    }
}
